package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.Result;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
import org.springframework.web.servlet.tags.EscapeBodyTag;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    //注入memberDao
    @Autowired
    private MemberDao memberDao;

    //注入OrderDao
    @Autowired
    private OrderDao orderDao;

    //获取运营数据报告
    public Result getBusinessReport() throws Exception {

        try {
            //创建Map集合用于存储响应前端的数据
            Map<String, Object> map = new HashMap<>();

            //根据日期工具类获取当前日期
            String reportDate = DateUtils.parseDate2String(DateUtils.getToday());
            //根据日期工具类获取本周一的日期
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //根据日期工具类获取本月的第一天
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

            //根据日期查询今日新增的会员数
            Integer todayNewMember = memberDao.findMemberCountByDate(reportDate);

            //根据日期查询本周新增的会员数
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);

            //根据日期查询本月新增的会员数
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

            //查询总会员数
            Integer totalMember = memberDao.findMemberTotalCount();

            //根据日期获取今日预约数
            Integer todayOrderNumber = orderDao.findOrderCountByDate(reportDate);

            //根据日期获取本周预约数
            Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);

            //根据日期获取本月预约数
            Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);

            //根据日期获取本日到诊人数
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(reportDate);

            //根据日期获取本周到诊人数
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountByDate(thisWeekMonday);

            //根据日期获取本月到诊人数
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountByDate(firstDay4ThisMonth);

            //获取热门套餐
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            //将数据添加进集合中
            map.put("reportDate", reportDate);
            map.put("todayNewMember", todayNewMember);
            map.put("thisWeekNewMember", thisWeekNewMember);
            map.put("thisMonthNewMember", thisMonthNewMember);
            map.put("totalMember", totalMember);
            map.put("todayOrderNumber", todayOrderNumber);
            map.put("thisWeekOrderNumber", thisWeekOrderNumber);
            map.put("thisMonthOrderNumber", thisMonthOrderNumber);
            map.put("todayVisitsNumber", todayVisitsNumber);
            map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
            map.put("hotSetmeal", hotSetmeal);

            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
